package dao

import (
	"data-system/db"
	"data-system/models"
	"fmt"
	"log"
)

func BatchSave(nodes []models.NodeData) error {
	return db.NewDB().BatchSave(nodes)
}

func Save(node models.NodeData) error {
	return db.NewDB().Save(node)
}

func Delete(nodeType string, id int64) error {
	node, err := QueryById(nodeType, id)
	if err != nil || node == nil {
		return fmt.Errorf("【%v - %v】数据不存在", nodeType, id)
	}
	return db.NewDB().DeleteById(*node)
}

func QueryAll(nodeType string) ([]models.NodeData, error) {
	return db.NewDB().QueryAll(nodeType)
}

func QueryById(nodeType string, id int64) (*models.NodeData, error) {
	nodeData, err := db.NewDB().QueryById(nodeType, id)
	if err != nil {
		log.Fatalf("查询失败：nodeType=%v，id=%d", nodeType, id)
		return nil, err
	}
	if nodeData.Id == 0 {
		return nil, nil
	}
	return &nodeData, nil
}
